package com.huamoxi.controller;

import com.huamoxi.client.UserServiceClient;
import com.huamoxi.entity.Menu;
import com.huamoxi.result.Result;
import com.huamoxi.service.MenuService;
import com.huamoxi.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private UserServiceClient userServiceClient;

    @GetMapping("/role/{roleId}")
    public Result getMenusByRoleId(@PathVariable("roleId") Long roleId) {
        if (roleId == null) {
            return ResultUtil.fail("参数错误");
        }
        return menuService.getMenusByRoleId(roleId);
    }

    @GetMapping("/user/{userId}")
    public Result getMenusByUserId(@PathVariable("userId") Long userId) {
        if (userId == null) {
            return ResultUtil.fail("参数错误");
        }
        return menuService.getMenusByUserId(userId);
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestHeader("Authorization") String authorization) {
        try {
            return userServiceClient.getUserInfo(authorization);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.fail(e.getMessage());
        }
    }
}
